package com.hspedu.homework.homework13;

public class Homework13 {
    public static void main(String[] args) {
        Person student = new Student("张三", "男",20,20200201);
        Person teacher = new Teacher("李四", "男", 40, 20);
        System.out.println(((Student) student).play());
        System.out.println(((Teacher) teacher).play());
        ((Student) student).study();
        ((Teacher) teacher).teach();


        /**
         * 7.定义多态数组，里面保存2个学生和2个教师，要求按年龄从高到底排序
         */
        Person[] people = new Person[4];
        people[0] = new Student("Jack", "男",25,20200201);
        people[1] = new Student("Rose", "女",20,20200202);
        people[2] = new Teacher("Smith", "男", 45, 20);
        people[3] = new Teacher("Milan", "女", 40, 20);
        //冒泡排序
        Person temp =new Person();//定义一个中间变量
        for (int i = 0; i < people.length-1; i++) {
            for (int j = 0; j < people.length-i-1; j++) {
                if (people[j].getAge()>people[j+1].getAge()){
                    temp = people[j];
                    people[j] =people[j+1];
                    people[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < people.length; i++) {
            System.out.println(people[i].getName());
        }


        Homework13 homework13 = new Homework13();
        homework13.test(student);
        homework13.test(teacher);
    }
    /**
     * 定义方法，形参为Person类型，功能：调用学生的study或教师的teach方法
     */
    public void test(Person person){
        if (person instanceof Student){
            ((Student) person).study();
        }else if (person instanceof Teacher){
            ((Teacher) person).teach();
        }else{
            System.out.println("不做处理");
        }
    }
}
